package com.itcast.googleplay.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RatioImageView extends ImageView {
	public static final String NAMESPACE = "http://schemas.android.com/apk/res/com.itcast.googleplay";
	private float ratio;
	public RatioImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initAttrs(attrs);
	}

	private void initAttrs(AttributeSet attrs) {
		ratio = attrs.getAttributeFloatValue(NAMESPACE, "ratio", 0);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		if(ratio>0){
			int size = MeasureSpec.getSize(widthMeasureSpec);
			int height = (int)(size/ratio+0.5f);
			
			heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
		}
	
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
}
